package com.gxuwz.medical.domain.reducttreat;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.chronicinfo.ChronicInfo;
import com.gxuwz.medical.exception.DbException;
import com.gxuwz.medical.tools.DateUtil;

/**
 * 报销单
 * 
 * @author L&P
 * 
 */
public class Reducttreat {
	// 自增id
	private Integer id;
	// 慢性病证编号
	private String chronicInfoId;
	// 治疗时间
	private String treattime;
	// 花费金额
	private Integer money;
	// 医院票号
	private String hosId;
	// 审核状态
	private Integer reductstatus;
	// 报销金额
	private Integer freemoney;
	// 慢病症对象
	private ChronicInfo chronicInfo;

	public Reducttreat() {

	}

	/**
	 * 全参构造函数
	 * 
	 * @param id
	 * @param chronicInfoId
	 * @param treattime
	 * @param money
	 * @param hosId
	 * @param reductstatus
	 * @param freemoney
	 * @param chronicInfo
	 */
	public Reducttreat(Integer id, String chronicInfoId, String treattime,
			Integer money, String hosId, Integer reductstatus,
			Integer freemoney, ChronicInfo chronicInfo) {
		
		this.id = id;
		this.chronicInfoId = chronicInfoId;
		this.treattime = treattime;
		this.money = money;
		this.hosId = hosId;
		this.reductstatus = reductstatus;
		this.freemoney = freemoney;
		this.chronicInfo = chronicInfo;
	}

	public Reducttreat(Integer id) throws Exception {
		this.id = id;
		try {
			loadDB();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DbException("无法找到ID" + this.id);

		}
	}

	// 加载信息
	private void loadDB() throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_reducttreat where  id=?");
			int index = 1;
			pstmt.setInt(index, this.id);
			rs = pstmt.executeQuery();
			if (rs.next()) { // 循环输出
				this.id = rs.getInt("id");
				this.chronicInfoId = rs.getString("chronicInfoId");
				this.treattime = rs.getString("treattime");
				this.money = rs.getInt("money");
				this.hosId = rs.getString("hosId");
				this.reductstatus = rs.getInt("reductstatus");
				this.freemoney = rs.getInt("freemoney");
				this.chronicInfo = new ChronicInfo(rs.getString("id"));
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}

	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getChronicInfoId() {
		return chronicInfoId;
	}

	public void setChronicInfoId(String chronicInfoId) {
		this.chronicInfoId = chronicInfoId;
	}

	public String getTreattime() {
		return treattime;
	}

	public void setTreattime(String treattime) {
		this.treattime = treattime;
	}

	public Integer getMoney() {
		return money;
	}

	public void setMoney(Integer money) {
		this.money = money;
	}

	public String getHosId() {
		return hosId;
	}

	public void setHosId(String hosId) {
		this.hosId = hosId;
	}

	public Integer getReductstatus() {
		return reductstatus;
	}

	public void setReductstatus(Integer reductstatus) {
		this.reductstatus = reductstatus;
	}

	public Integer getFreemoney() {
		return freemoney;
	}

	public void setFreemoney(Integer freemoney) {
		this.freemoney = freemoney;
	}

	public ChronicInfo getChronicInfo() {
		return chronicInfo;
	}

	public void setChronicInfo(ChronicInfo chronicInfo) {
		this.chronicInfo = chronicInfo;
	}

	public void addReducttreat() throws Exception {
		Connection conn = null;
		try {

			conn = DbUtil.getConn();
			// 2:持久化数据
			saveToDB(conn);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			// 关闭连接
			DbUtil.close(conn);
		}

	}

	private void saveToDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"INSERT INTO t_reducttreat (chronicInfoId, treattime, money, hosId,reductstatus,freemoney)");
			sqlBuff.append("VALUES (?, ?, ?, ?,?,?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			int index = 1;
			pstmt.setString(index++, this.chronicInfoId);
			pstmt.setString(index++, this.treattime);
			pstmt.setInt(index++, this.money);
			pstmt.setString(index++, this.hosId);
			pstmt.setInt(index++, this.reductstatus);
			pstmt.setInt(index++, this.freemoney);
			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}

}
